﻿Imports System.Text

Namespace DotNetNuke.Modules.ActiveForums.Controls
    Public Class ForumContent
        Public Enum GroupingOptions
            None
            Categories
            Forums
        End Enum
        Public Property PortalId() As Integer = -1
        Public Property ModuleId() As Integer = -1
        Public Property TabId() As Integer = -1
        Public Property ForumId() As Integer = -1
        Public Property ForumGroupId() As Integer = -1
        Public Property ParentForumId() As Integer = -1
        Public Property GroupBy() As GroupingOptions = GroupingOptions.None
        Public Property TopicId() As Integer = -1
        Public Property Topic() As String = String.Empty
        Public Property ItemTemplate() As String = String.Empty
        Public Property HeaderTemplate() As String = String.Empty
        Public Property FooterTemplate() As String = String.Empty
        Public Property ForumUser() As User
        Public Property IncludeClasses() As Boolean = True
        Public Function Render() As String
            Dim sb As New StringBuilder
            Dim db As New Data.CommonDB
            Dim sHost As String = Utilities.GetHost()
            If sHost.EndsWith("/") Then
                sHost = sHost.Substring(0, sHost.Length - 1)
            End If
            Dim ctlUtils As New ControlUtils
            Dim forumPrefix As String = String.Empty
            Dim groupPrefix As String = String.Empty
            'Dim _forumGroupId As Integer = -1
            If ParentForumId = -1 Then
                ParentForumId = ForumId
            End If
            Dim groupTemplate As String = TemplateUtils.GetTemplateSection(ItemTemplate, "[AF:DIR:FORUMGROUP]", "[/AF:DIR:FORUMGROUP]")
            Dim forumTemplate As String = TemplateUtils.GetTemplateSection(ItemTemplate, "[AF:DIR:FORUM]", "[/AF:DIR:FORUM]")
            Dim subForumTemplate As String = TemplateUtils.GetTemplateSection(ItemTemplate, "[AF:DIR:SUBFORUM]", "[/AF:DIR:SUBFORUM]")
            Dim useFriendlyUrl As Boolean = Utilities.IsRewriteLoaded
            Dim currGroup As Integer = -1
            Dim gtmp As String = String.Empty
            Dim ftmp As String = String.Empty
            Dim subtmp As String = String.Empty
            Using dr As IDataReader = db.ForumContent_List(PortalId, ModuleId, ForumGroupId, ForumId, ParentForumId)
                'ParentForum Section
                While dr.Read
                    Dim sURL As String = ctlUtils.BuildUrl(TabId, ModuleId, dr("GroupPrefixURL").ToString, dr("PrefixURL").ToString, Integer.Parse(dr("ForumGroupId").ToString), Integer.Parse(dr("ForumId").ToString), -1, -1, String.Empty, 1, -1)
                    If IncludeClasses Then
                        sb.Append("<div class=""fcv-header""><a href=""" & sURL & """><span>" & dr("ForumName").ToString & "</span></a></div>")
                    Else
                        sb.Append("<div><a href=""" & sURL & """><span>" & dr("ForumName").ToString & "</span></a></div>")
                    End If

                    forumPrefix = dr("PrefixURL").ToString
                    groupPrefix = dr("GroupPrefixURL").ToString
                    '  _forumGroupId = Integer.Parse(dr("ForumGroupId").ToString)
                End While
                'SubForums
                dr.NextResult()
                Dim subForumCount As Integer = 0
                Dim sSubforums As String = String.Empty
                While dr.Read()
                    If Permissions.HasPerm(dr("CanRead").ToString, ForumUser.UserRoles) Then
                        Dim sURL As String = ctlUtils.BuildUrl(TabId, ModuleId, dr("GroupPrefixURL").ToString, dr("PrefixURL").ToString, Integer.Parse(dr("ForumGroupId").ToString), Integer.Parse(dr("ForumId").ToString), -1, -1, String.Empty, 1, -1)
                        If ForumId = Integer.Parse(dr("ForumId").ToString) Then
                            sSubforums &= "<li class=""fcv-selected"">"
                        Else
                            sSubforums &= "<li>"
                        End If

                        sSubforums &= "<a href=""" & sURL & """><em></em><span>" & dr("ForumName").ToString & "</span></a></li>"
                        If IncludeClasses Then
                            sSubforums &= "<li class=""fcv-desc"">" & dr("ForumDesc").ToString & "</li>"
                        Else
                            sSubforums &= "<li>" & dr("ForumDesc").ToString & "</li>"
                        End If

                    End If

                End While
                If Not String.IsNullOrEmpty(sSubforums) Then
                    If IncludeClasses Then
                        sb.Append("<ul class=""fcv-subforums"">")
                    Else
                        sb.Append("<ul>")
                    End If

                    sb.Append(sSubforums)
                    sb.Append("</ul>")
                End If
                'Topics in ParentForum
                dr.NextResult()
                Dim catKey As String = String.Empty
                Dim count As Integer = 0
                Dim catCount As Integer = 0
                While dr.Read()
                    If catKey <> dr("CategoryName").ToString & dr("CategoryId").ToString Then
                        If count > 0 Then
                            sb.Replace("[CATCOUNT]", catCount.ToString)
                            sb.Append("</ul></div>")
                            count = 0
                            catCount = 0
                        End If
                        If IncludeClasses Then
                            sb.Append("<div class=""fcv-categorysection""><div class=""fcv-categoryname""><span class=""fcv-catcount"">[CATCOUNT]</span>" & dr("CategoryName").ToString & " </div>")
                            sb.Append("<ul class=""fcv-topicslist"">")
                        Else
                            sb.Append("<div><div><span>[CATCOUNT]</span>" & dr("CategoryName").ToString & " </div>")
                            sb.Append("<ul>")
                        End If
                        
                        catKey = dr("CategoryName").ToString & dr("CategoryId").ToString
                    End If
                    'Dim Params As String() = {"aff=" & ForumId, "fcc=" & dr("TopicId").ToString}
                    If TopicId = CInt(dr("TopicId").ToString) Then
                        sb.Append("<li class=""fcv-selected"">")
                    Else
                        sb.Append("<li>")
                    End If
                    catCount += 1
                    'Dim Params As String() = {ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId, ParamKeys.ViewType & "=topic"}
                    Dim Params As String() = {ParamKeys.TopicId & "=" & dr("TopicId").ToString}
                    Dim sTopicURL As String = ctlUtils.BuildUrl(TabId, ModuleId, groupPrefix, forumPrefix, ForumGroupId, ForumId, Integer.Parse(dr("TopicId").ToString), dr("URL").ToString, -1, -1, String.Empty, 1, -1)
                    sb.Append("<a href=""" & sTopicURL & """><span>" & dr("Subject").ToString & "</span></a></li>")
                    If TopicId > 0 Then
                        If CInt(dr("TopicId").ToString) = TopicId Then
                            '  RenderTopic(dr)
                        End If
                    End If

                    count += 1
                End While
                sb.Replace("[CATCOUNT]", catCount.ToString)
                If count > 0 Then
                    sb.Append("</ul></div>")
                End If
                dr.Close()
            End Using
            Return sb.ToString
        End Function
        'Private Sub RenderTopic(ByVal dr As IDataRecord)
        '    Dim topicsTemplate As String = TemplateUtils.GetTemplateSection(TopicTemplate, "[TOPIC]", "[/TOPIC]")
        '    Dim replyTemplate As String = TemplateUtils.GetTemplateSection(TopicTemplate, "[REPLIES]", "[/REPLIES]")
        '    TopicTemplate = TemplateUtils.ReplaceSubSection(TopicTemplate, String.Empty, "[REPLIES]", "[/REPLIES]")
        '    topicsTemplate = topicsTemplate.Replace("[BODY]", dr("Body").ToString)
        '    topicsTemplate = topicsTemplate.Replace("[ACTIONS:DELETE]", String.Empty)
        '    topicsTemplate = topicsTemplate.Replace("[ACTIONS:EDIT]", String.Empty)
        '    topicsTemplate = topicsTemplate.Replace("[ACTIONS:ALERT]", String.Empty)
        '    topicsTemplate = topicsTemplate.Replace("[ATTACHMENTS]", String.Empty)
        '    topicsTemplate = topicsTemplate.Replace("[SIGNATURE]", String.Empty)
        '    TopicTemplate = TemplateUtils.ReplaceSubSection(TopicTemplate, topicsTemplate, "[TOPIC]", "[/TOPIC]")


        '    TopicTemplate = TopicTemplate.Replace("[ADDREPLY]", String.Empty)
        '    TopicTemplate = TopicTemplate.Replace("[POSTRATINGBUTTON]", String.Empty)
        '    TopicTemplate = TopicTemplate.Replace("[TOPICSUBSCRIBE]", String.Empty)
        '    Topic = TopicTemplate

        'End Sub
    End Class
End Namespace

